package org.linlinjava.litemall.admin.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.dto.AdminSysMsgsDTO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallSysMsg;
import org.linlinjava.litemall.db.service.LitemallSysMsgService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminSysMsgService {
    private final LitemallSysMsgService sysMsgService;

    public ResponseUtil.Response sysMsgs(AdminSysMsgsDTO sysMsgsDTO) {
        IPage<LitemallSysMsg> sysMsgs = sysMsgService.page(new Page<>(sysMsgsDTO.getPage(), sysMsgsDTO.getLimit()),Wrappers.lambdaQuery(LitemallSysMsg.class)
                .eq(sysMsgsDTO.getStatus()!=null,LitemallSysMsg::getStatus,sysMsgsDTO.getStatus())
                .eq(sysMsgsDTO.getType()!=null,LitemallSysMsg::getType,sysMsgsDTO.getType())
                .eq(StrUtil.isNotBlank(sysMsgsDTO.getService()),LitemallSysMsg::getService,sysMsgsDTO.getService())
                        .and(wrapper-> wrapper
                                .isNotNull(LitemallSysMsg::getId)
                                .like(StrUtil.isNotBlank(sysMsgsDTO.getDesc()),LitemallSysMsg::getName,sysMsgsDTO.getDesc()).or()
                                .isNotNull(LitemallSysMsg::getId)
                                .like(StrUtil.isNotBlank(sysMsgsDTO.getDesc()),LitemallSysMsg::getContent,sysMsgsDTO.getDesc()).or()
                                .isNotNull(LitemallSysMsg::getId)
                                .like(StrUtil.isNotBlank(sysMsgsDTO.getDesc()),LitemallSysMsg::getTitle,sysMsgsDTO.getDesc())
                        )
                .orderByDesc(LitemallSysMsg::getAddTime));
        return ResponseUtil.okPage(sysMsgs);
    }

    public ResponseUtil.Response status(Integer id) {
        LitemallSysMsg sysMsg = sysMsgService.getById(id);
        sysMsg.setStatus(!sysMsg.getStatus());
        return sysMsgService.updateById(sysMsg)?ResponseUtil.ok():ResponseUtil.fail();
    }

    public ResponseUtil.Response edit(LitemallSysMsg sysMsg) {
        return sysMsgService.updateById(sysMsg)?ResponseUtil.ok():ResponseUtil.fail();
    }
}
